package com.liang.blog.controller;

import com.liang.blog.model.BlogInfo;
import com.liang.blog.service.BlogService;
import com.liang.blog.utils.JwtUtils;
import io.jsonwebtoken.lang.Assert;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
@Slf4j
@RequestMapping("/blog")
@RestController
public class BlogController {
    @Autowired
    private BlogService blogService;
    @RequestMapping("/getList")
    public List<BlogInfo> getList() {
       return blogService.getList();
    }
    @RequestMapping("/getBlogDetail")
    public BlogInfo getBlogDetail(Integer blogId,HttpServletRequest request) {
//        if(blogId==null) {
            log.info("getBlogDetail 接收参数:blogId: " +blogId);
//            return null;//不合适
           // throw new UnsupportedOperationException("Id不能为null");//自己定义一个异常
       Assert.notNull(blogId,"blogId不能为null");
       BlogInfo blogInfo = blogService.getBlogDetail(blogId);
       //作者ID
        Integer authorId = blogInfo.getUserId();
        //登录用户ID
        String userToken = request.getHeader("user_token_header");
        Integer userId = JwtUtils.getUserIdFromToken(userToken);
        if(authorId!=null && authorId==userId) {
            blogInfo.setLoginUser(true);
        }
        return blogInfo;

//        }
//       return blogService.getBlogDetail(blogId);
    }
    /**
     * 发布博客
     */
    @RequestMapping("/add")
    public Boolean addBlog(@Validated @RequestBody BlogInfo blogInfo ,HttpServletRequest request) { //json需要用@RequestBody接收
       log.info("addBlog 接收参数 ：" + blogInfo);
        //获取userId
        String userToken = request.getHeader("user_token_header");
        Integer userId = JwtUtils.getUserIdFromToken(userToken);
        Assert.notNull(userId,"用户未登录");//大概率不会为null,可以去掉
        blogInfo.setUserId(userId );
        Integer result = null;
       try{
           result = blogService.insertBlog(blogInfo);
           if(result == 1) {
               return true;
           }
       }catch (Exception e) {
           log.error("发布博客失败，e: " +e);
       }
        return false;
    }
/**
 * 更新博客
 */
   @RequestMapping("/updateBlog")
   public Boolean updateBlog(@Validated @RequestBody BlogInfo blogInfo) {
       log.info("updateBlog 接收参数：" + blogInfo);
       Integer result =  null;
      try {
          result = blogService.update(blogInfo);
          if(result <= 0) {
              return false;
          }
      }catch (Exception e) {
          log.error("updateBlog 发生异常，e:  ",e);
      }
      return true;
   }

   @RequestMapping("/deleteBlog")
   public Boolean deleteBlog(Integer blogId) {
       Assert.notNull(blogId,"删除的博客ID不能为null");
       log.info("deleteBlog 接收参数：" + blogId);
       Integer result =  null;
       try {
           BlogInfo blogInfo = new BlogInfo();
           blogInfo.setId(blogId);
           blogInfo.setDeleteFlag(1);
           result = blogService.update(blogInfo);
           if(result <= 0) {
               return false;
           }
       }catch (Exception e) {
           log.error("deleteBlog 发生异常，e:  ",e);
       }
       return true;
   }


}
